import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:qgchat/common/action/filter/index.dart';
import 'package:qgchat/common/class/eventBus/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/common.dart';
import 'package:qgchat/http/models/resModel.dart';
import 'package:qgchat/page/live/dynamic/BloC/commentBloC.dart';
import 'package:qgchat/page/live/dynamic/BloC/dynamicBloC.dart';
import 'package:qgchat/widgets/due/dif.dart';
import 'package:qgchat/widgets/emoji/emoji.dart';
import '../BloC/emojiBloC.dart';

///
/// 通话中输入框组件
///

class DynamicInput extends StatefulWidget {
  DynamicInput(this.id, {Key key}) : super(key: key);

  final int id;

  @override
  _DynamicInputState createState() => _DynamicInputState();
}

class _DynamicInputState extends State<DynamicInput> {
  TextEditingController c = TextEditingController(); // 输入框控制器
  final FocusNode focus = FocusNode(); // 焦点控制
  String placeholder = '请输入消息...'; // 提示文字
  int pid; // 评论回复的id

  @override
  void initState() {
    super.initState();
    bus.on("dtfocus", (data) {
      placeholder = "回复：${data['nickname']}";
      pid = data['pid'];
      setState(() {});
      focus.unfocus();
      focus.requestFocus();
    });
    focus.addListener(() {
      if (focus.hasFocus) {
        BlocProvider.of<EmojiBloc>(context).add(EmojiEvent.hide);
      }
    });
  }

  @override
  void dispose() {
    bus.off("dtfocus");
    super.dispose();
  }

  // 发送
  send() async {
    ResModel res = await CommonApis.sendDtComment(widget.id, Filter.txt(c.text), pid: pid);
    if (res.status == 0) return;
    showToast("评论成功");
    focus.unfocus();
    c.text = "";
    BlocProvider.of<DynamicBloc>(context).add(DynamicEvent.refresh);
    BlocProvider.of<CommentBloc>(context).add(CommentEvent.refresh);
    BlocProvider.of<EmojiBloc>(context).add(EmojiEvent.hide);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          // 输入框
          Container(
            width: w100(context),
            height: px(98),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                top: BorderSide(
                  width: 1,
                  color: col("edeff2"),
                ),
              ),
            ),
            child: Row(
              children: <Widget>[
                // 消息输入框
                inputText(),
                // emoji按钮
                emojiBtn(),
                // 分割线
                shu,
                // 发送按钮
                sendBtn(),
              ],
            ),
          ),
          // emoji
          Container(
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                top: BorderSide(
                  width: 1,
                  color: col("edeff2"),
                ),
              ),
            ),
            child: BlocBuilder<EmojiBloc, bool>(
              builder: (context, state) {
                return If(
                  dif: state,
                  ifView: Emoji.widget(context, (String txt) {
                    setState(() {
                      String val = "${c.text}$txt";
                      c.text = val;
                      c.selection = TextSelection.collapsed(
                        offset: val.length,
                      );
                    });
                  }),
                  elseView: Container(),
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  /// 消息输入框
  inputText() {
    return Expanded(
      child: Padding(
        padding: EdgeInsets.only(left: px(30)),
        child: CupertinoTextField(
          controller: c,
          focusNode: focus,
          placeholder: placeholder,
          placeholderStyle: TextStyle(
            color: col("bfc2cc"),
            fontSize: px(32),
          ),
          padding: EdgeInsets.zero,
          decoration: BoxDecoration(
            border: null,
          ),
        ),
      ),
    );
  }

  /// emoji按钮
  emojiBtn() {
    return Container(
      width: px(60),
      height: px(60),
      margin: EdgeInsets.symmetric(horizontal: px(20)),
      child: FlatButton(
        onPressed: () {
          focus.unfocus();
          BlocProvider.of<EmojiBloc>(context).add(EmojiEvent.toggle);
        },
        padding: EdgeInsets.zero,
        child: Image.asset(
          "assets/images/icon/smile.png",
        ),
      ),
    );
  }

  /// 竖直分割线
  final shu = Container(
    width: 1,
    height: px(42),
    color: col("edeff2"),
  );

  /// 发送按钮
  sendBtn() {
    return Container(
      width: px(122),
      child: FlatButton(
        onPressed: send,
        padding: EdgeInsets.zero,
        child: Text(
          "发送",
          style: TextStyle(
            color: col("303133"),
            fontSize: px(32),
          ),
        ),
      ),
    );
  }
}
